package burubaka.bat;

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import burubaka.BurubakaConstants;
import burubaka.model.RaceInfo;
import burubaka.model.TotalResult;
import burubaka.service.TotalResultService;

import com.google.appengine.api.datastore.Key;

public class RankingOrderBat {

	private static final Logger log = Logger.getLogger(RankingOrderBat.class.getName());
	private TotalResultService totalResultService = new TotalResultService();

	public RaceInfo execute() throws Exception {

		List<TotalResult> list = totalResultService.getRanking(BurubakaConstants.CURRENT_YEAR);
		Map<Key, String> withdrawalMap = totalResultService.getRankingMapForWithdrawal();
		Map<Key, String> withdrawalRateMap = totalResultService.getRankingMapForWithdrawalRate();
		Map<Key, String> hitMap = totalResultService.getRankingMapForHit();
		Map<Key, String> hitRateMap = totalResultService.getRankingMapForHitRate();
		for (TotalResult totalResult : list) {

			if (totalResult.getBets() == 0) {
				continue;
			}

			Key key = totalResult.getKey();
			// 回収額順位設定
			String withdrawalOrder = withdrawalMap.get(key);
			totalResult.setLastWithdrawalOrder(totalResult.getWithdrawalOrder());
			totalResult.setWithdrawalOrder(withdrawalOrder);

			// 回収率順位設定
			String withdrawalRateOrder = withdrawalRateMap.get(key);
			totalResult.setLastWithdrawalRateOrder(totalResult.getWithdrawalRateOrder());
			totalResult.setWithdrawalRateOrder(withdrawalRateOrder);

			// 的中数順位設定
			String hitOrder = hitMap.get(key);
			totalResult.setLastHitOrder(totalResult.getHitOrder());
			totalResult.setHitOrder(hitOrder);

			// 的中率順位設定
			String hitRateOrder = hitRateMap.get(key);
			totalResult.setLastHitRateOrder(totalResult.getHitRateOrder());
			totalResult.setHitRateOrder(hitRateOrder);

			totalResultService.regist(totalResult);
		}

		return null;
	}
}
